﻿using KPI.Api;
using KPI.Comm;
using KPI.Data;
using KPI.Entity;
using KPI.UIBase;
using CssdAssistantClient.ViewModels;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using TYBase;
using WPF_TY_UI;
using System.Threading;

namespace CssdAssistantClient.Views
{
    /// <summary>
    /// ViewLogin.xaml 的交互逻辑
    /// </summary>
    public partial class ViewLogin : TYWindow
    {
        ViewLoginModel vm = new ViewLoginModel();
        public ViewLogin()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += ViewLogin_Loaded;
        }

        private void ViewLogin_Loaded(object sender, RoutedEventArgs e)
        {
            //{
            //    string data = "e29ddb1ffe71a585ec9282f262c1d8b38e2f3601b6898d82ab0b1d6f25ad5acce38baefaa1eaadc2611bd87a15c60593c416b9a47a1114850a737c4f1f6ee4fc";
            //    var res = SecurityUtil.decryptSm4Base64(data, KPIData.appSecret);

            //}
            //var rData = await Run(() =>
            //{
            //    return QuesionNaireApi.GetQuestionNaireByUserAndOffice("1015", "69");
            //});

            //if (rData == null)
            //{
            //    MessageBox.Show("数据异常,请检查日志!");
            //    return;
            //}
            //if (rData.code != 0)
            //{
            //    MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
            //    return;
            //}
            //if (rData.data != null)
            //{
            //    var data = rData.data.list.FirstOrDefault(x => x.userExamStatus == 0);
            //    if (data != null)
            //    {
            //        var rData2 = await Run(() =>
            //        {
            //            return QuesionNaireApi.GetQuestionNareInfo(data.examId);
            //        });

            //        if (rData2 == null)
            //        {
            //            MessageBox.Show("数据异常,请检查日志!");
            //            return;
            //        }
            //        if (rData2.code != 0)
            //        {
            //            MessageBox.Show("数据异常,code:" + rData2.code + ":msg:" + rData2.msg);
            //            return;
            //        }
            //        if (rData2.data != null)
            //        {
            //            ExamUserInfoDto examUserInfo = new ExamUserInfoDto()
            //            {
            //                userId = "1015",
            //                examId = rData2.data.examId,
            //                OfficeCode = "45",
            //                listUserAnswer = new System.Collections.Generic.List<ExamUserAnswerInfoDto>()
            //            };
            //            foreach (var item in rData2.data.listHistoryQuestion)
            //            {
            //                examUserInfo.listUserAnswer.Add(new ExamUserAnswerInfoDto
            //                {
            //                    userId = "1015",
            //                    qbId = item.qbId,
            //                    answerId = item.listItemInfo.FirstOrDefault()?.qbItemId,
            //                    examId = rData2.data.examId
            //                });
            //            }

            //            var rData3 = await Run(() =>
            //            {
            //                return QuesionNaireApi.AddAnswerForUser(examUserInfo);
            //            });
            //        }
            //    }
            //}
        }

        private async void BtnClick_Login(object sender, RoutedEventArgs e)
        {
            LoginInfoDto loginInfo = new LoginInfoDto();
            loginInfo.userId = txtUserId.Text;
            loginInfo.password = MD5Helper.EncryptString(pwd.Password);
            loginInfo.officeCode = vm.SelectedOffice.Key;
            var rData = await Run(() => LoginApi.Login(loginInfo));
            {
                Thread.Sleep(100);
                var rData2 = await Run(() => LoginApi.GetOfficeList(loginInfo.userId));
            }

            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            if (rData.data != null)
            {
                KPIData.user = rData.data;
                KPIData.user.tokenId = rData.data.tokenId;
                this.DialogResult = true;
            }
        }

        private void Test(IEnumerable<string> list)
        {
            var data = list.ElementAtOrDefault(0);
        }
        private async void txtUserId_LostFocus(object sender, RoutedEventArgs e)
        {
            //SecurityUtil.decryptSm4Base64(vo.data, KPIData.appSecret);
            string v = SecurityUtil.decryptSm4Base64("8206821d27bd9c65715e25245b9e97db14a6d6ecb32ed973c0ef6b043816c1c782acd1b2619262b21d5db6ca0bf9d7de1c2b75ce6cff85fa7f15195daf2a5efc19ff1727e11f86988b10a0bcb30882c3cb77336439e60b9f52b899e5007648b3a2145a16bc17a993bb9bd6ca925ba0401305c580ee708cdb3302adcdb47105486dd3c82cbeff4f742fb7a8919760bc9107607c218e019833c6d01fe8ba0db8ba4911bebc85714cc0ed028241299a6b546e2a2eb44ac6b71d493108b5abb184a8da20f8b11e62af5e5020656d170b51279fcfd69b8abe06edfec89fb4bb40a3baa54a68f75942e6d71dc750fbec693c2747e6ccbec9d0654d7e2daa362e49fa2e", KPIData.appSecret);
            vm.ListOffice.Clear();
            string userId = txtUserId.Text;
            if (userId.NotNull())
            {
                var rData = await Run(() => LoginApi.GetOfficeList(userId));

                if (rData == null)
                {
                    MessageBox.Show("数据异常,请检查日志!");
                    return;
                }
                if (rData.code != 0)
                {
                    MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                    return;
                }
                if (rData.data != null)
                {
                    foreach (var item in rData.data)
                    {
                        vm.ListOffice.Add(new StringStringData { Key = item.officeCode, Value = item.officeName });
                    }
                }
            }
        }
    }
}
